<?php

namespace application\common\controller;

use ticky\response;

define('IN_ADMIN', true);

class admin extends common {

    protected $uid = 0;
    protected $username = '';
    protected $groupid = 0;
    protected $admin = 0;
    protected $roleid = 0;
    protected $siteid = 1;
    protected $skin = 'blue';

    public function __construct() {

        parent::__construct();
        $this->uid = session('uid');
        $this->username = session('username');
        $this->groupid = session('groupid');
        $this->adminid = session('adminid');
        $this->roleid = session('roleid');
        $this->siteid = get_siteid();
        $this->skin = !isset($_COOKIE['ticky_tickyskin']) ? 'blue' : $_COOKIE['ticky_tickyskin'];
        $site = get_siteinfo($this->siteid);
        if (empty($site['domain'])) {
            $site['domain'] = "/";
        }

        //ajax验证信息不需要登录
        if (substr(ACTION_NAME, 0, 7) != 'public_') {
            $this->check_admin();
            $this->check_priv();

            $this->assign('uid', $this->uid);
            $this->assign('username', $this->username);
            $this->assign('groupid', $this->groupid);
            $this->assign('adminid', $this->adminid);
            $this->assign('skin', $this->skin);
            $this->assign('site', $site);
        }
    }

    /**
     * 判断用户是否已经登陆
     */
    private function check_admin() {
        if (MODULE_PATH == 'admin' && CONTROLLER_NAME == 'login' && ACTION_NAME == 'index') {
            return true;
        } else {
            //判断是否登录
            if (!$this->uid) {
                showmsg(L('login_website'), url('admin/login/index'));
            }
        }
    }

    /**
     * 权限判断
     */
    private function check_priv() {

    }

    /**
     *  当前位置/admin/auth/breadcrumb/?m=admin&c=friendlink&a=index
     */
    public function breadcrumb() {
        $m = $_GET['model'];
        $c = $_GET['controller'];
        $a = $_GET['action'];
        $child = db('menu')->where(['m' => $m, 'c' => $c, 'a' => $a])->select();
        $str = '<li><i class="fa fa-home fa-fw"></i>首页</li>';
        $root_str = '';
        $child_str = '';
        $url = "/" . $m . "/" . $c;
        if (is_array($child) && count($child) > 0) {
            $icon = '';
            if (!empty($child[0]['icon'])) {
                $icon = '<i class="fa ' . $child[0]['icon'] . ' fa-fw"></i>';
            }
            $child_str .= '<li><a target="main" href="' . $url . '">' . $icon . $child[0]['name'] . '</a></li>';
            $root = db('menu')->where(['id' => $child[0]['parentid']])->find();
            if (is_array($root)) {
                if (!empty($root['icon'])) {
                    $icon = '<i class="fa ' . $root['icon'] . ' fa-fw"></i>';
                }
                $root_str .= '<li>' . $icon . $root['name'] . '</li>';
            }
            $root = db('menu')->where(['id' => $root['parentid']])->find();
            if (is_array($root)) {
                if (!empty($root['icon'])) {
                    $icon = '<i class="fa ' . $root['icon'] . ' fa-fw"></i>';
                }
                $root_str .= '<li>' . $icon . $root['name'] . '</li>';
            }
        }
        response::ajax($str . $root_str . $child_str . "<li id='refresh' onclick=\"parent.main.location.reload();\"><i class='fa fa-refresh'></i></li>");
    }

}
